var r = null;
import store from '@/vuex/store'
var route = '_doc/chataudio/';//存放音频
function startReady() {
  r = plus.audio.getRecorder()
  if (r == null) {
    alert('未准备好')
    return
  }
  r.record({filename: route}, function (p) {
//  alert('录制成功'+ p)
    plus.io.resolveLocalFileSystemURL(p, function (entry) {
      base64(entry.toLocalURL());
    }, function (e) {

    })
  }, function (e) {
    console.log(e)
    alert('录制失败' + e.message)
  })
}

function base64(url) {
  plus.io.resolveLocalFileSystemURL(url, function (entry) {
    entry.file(function (file) {
      let fileReader = new plus.io.FileReader();
      fileReader.readAsDataURL(file);
      fileReader.onloadend = function (p) {
        let imgbase64 = p.target.result.toString();
        store.commit("base64",imgbase64)
        return imgbase64
      }
    })
  });
};

//停止录音
function stopReady() {
  r.stop()
}


function startPlay(url) {
  var p ={
    s:plus.audio.createPlayer(url),//播放对象
    r:url//播放路径
  } 
  store.commit("auobj",p)
  // setTimeout(() => {
  //   let len = p.getDuration() //音频长度、单位s
  //   console.log(len+"秒")
  // }, 500);
  // p.play(msg=>{
  //   store.commit("playStatus",0)
  //   alert("正在播放")
  // })
}

export default {
  startReady,
  stopReady,
  startPlay
}
